package pers.sloera.leetcode.editor.cn;

import cn.hutool.core.lang.Assert;

import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * 使数组中所有元素都等于零
 *
 * @author sloera
 * @date 2023-02-24 08:52:25
 */
class MakeArrayZeroBySubtractingEqualAmounts {
    public static void main(String[] args) {
        Solution solution = new MakeArrayZeroBySubtractingEqualAmounts().new Solution();
        Assert.isTrue(solution.minimumOperations(new int[]{1, 5, 0, 3, 5}) == 3);
        Assert.isTrue(solution.minimumOperations(new int[]{0}) == 0);
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public int minimumOperations(int[] nums) {
            // 排序，每个独立的非零元素都需要被减一次
            return Arrays.stream(nums).filter(x -> x != 0).boxed().collect(Collectors.toSet()).size();
        }

        public int minimumOperations1(int[] nums) {
            Arrays.sort(nums);
            int count = 0;
            for (int i = 0; i < nums.length; i++) {
                if (nums[i] == 0) {
                    continue;
                }
                count++;
                for (int inner = i + 1; inner < nums.length; inner++) {
                    nums[inner] -= nums[i];
                }
            }
            return count;
        }
    }
    //leetcode submit region end(Prohibit modification and deletion)

}
